package com.hx.ycy.web.websocket;

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Created by hx-pc on 16-12-22.
 */
@ServerEndpoint("/ycyChat")
@Component
public class ChatBarController extends NoticeMessage implements ChatBarStyleOfColor,ChatBarStyleOfImgHeader {

    private Map<String, String> colorMap = new LinkedHashMap<>();
    private Map<String, String> imgHeaderMap = new LinkedHashMap<>();
    private Map<String, String> backgroundMap = new LinkedHashMap<>();
    private static AtomicInteger colorCount = new AtomicInteger(0);

    @Override
    @OnOpen
    public void onOpen(Session session) {
        List<String> colorList = getChatBarColor();
        List<String> imgHeaderList = getImgHeader();
        colorMap.put(session.getId(), colorList.get(colorCount.get()));
        randomStyle(session.getId(), colorList, backgroundMap);
        randomStyle(session.getId(), imgHeaderList, imgHeaderMap);
        colorCount.getAndAdd(1);
        super.onOpen(session);
    }

    private void randomStyle(String sessionId, List<String> resourceList, Map<String, String> map) {
        String value;
        while (true) {
            int background = (int) (Math.random() * resourceList.size() - 1);
            value = resourceList.get(background == colorCount.get() ? 0 : background);//字，背景不重复
            if (!map.containsValue(value)){
                break;
            }
        }
        map.put(sessionId, value);
    }

    @Override
    @OnClose
    public void onClose(Session session) {
        super.onClose(session);
    }

    @Override
    @OnMessage
    public void onMessage(Session session, String message) {
        LOGGER.info("来自客户端的消息: sessionId :{}, message :{}", session.getId(), message);
        //发送给其他人
        AtomicReference<ChatResponse> responseAtomicReference = getChatResponseAtomicReference(session, message);
        responseAtomicReference.get().setTextAlign("left");
        sendApiUpdateMessageAllWithoutSelf(responseAtomicReference.get(), session);
        //发送给自己
        AtomicReference<ChatResponse> responseAtomicReferenceSelf = getChatResponseAtomicReference(session, message);
        responseAtomicReferenceSelf.get().setTextAlign("right");
        sendApiUpdateMessage(responseAtomicReferenceSelf.get(), session);
    }

    private AtomicReference<ChatResponse> getChatResponseAtomicReference(Session session, String message) {
        AtomicReference<ChatResponse> responseAtomicReference = new AtomicReference<>(new ChatResponse());
        responseAtomicReference.get().setMessage(message);
        responseAtomicReference.get().setColor(colorMap.get(session.getId()));
        responseAtomicReference.get().setBackground(backgroundMap.get(session.getId()));
        responseAtomicReference.get().setImgHeader(imgHeaderMap.get(session.getId()));
        return responseAtomicReference;
    }

}
